package cuit.blog.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cuit.blog.entity.User;
import cuit.blog.dao.UserRepository;
import cuit.blog.service.UserService;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	UserService userService;
	
	@Autowired
    UserRepository userRespository;
	
	int pageSize = 10;

	 @GetMapping("/login")  
	   public String login() {  
	   return "login"; // 返回登录页面的视图  
	    }  
	@PostMapping("/login")
	public String login(User user, HttpServletRequest request,Model model) {
		String username = user.getUsername();
		String pwd = user.getPwd();
		User loginUser = userService.login(username, pwd);
		System.out.println(">>>:" + username + ",>>>" + pwd);
		if (loginUser != null) {
			request.getSession().setAttribute("LoginUser", loginUser);
			System.out.println("牛魔王王中王" );
			if(username.equals("root")) {
				System.out.println("什么东西" );
				return "root/index";
			}else {
			return "redirect:/index";
			}
		}
		model.addAttribute("loginFaile", "用户名或密码错误");
		return "login";
	}

	@GetMapping("/findAll")
	public String findAll(Model model) {
		Page<User> page = userService.findAllByPage(0, pageSize);
		model.addAttribute("page", page);
		return "user/list";
	}

	@GetMapping("/page")
	public String findByPage(@RequestParam("pageIndex") int pageIndex, Model model) {
		Page<User> page = userService.findAllByPage(pageIndex, pageSize);
		model.addAttribute("userPage", page);
		return "user/list";
	}
	// 显示注册页面  
    @GetMapping("/register")  
    public String showRegisterForm() {  
        return "register"; // 返回注册页面的视图名  
    }  

    // 处理注册请求  
    @PostMapping("/register")  
    public String registerUser(@RequestParam String username,  
                               @RequestParam String password,  
                               @RequestParam String email,  
                               @RequestParam String phone,  
                               @RequestParam(required = false) String qq,  
                               @RequestParam(required = false) String info,  
                               Model model) {  

        // 检查用户名是否已经存在  
        if (userRespository.findByUsername(username) != null) {  
            model.addAttribute("error", "用户名已被占用，请选择其他用户名。");  
            return "register"; // 返回注册页面，并显示错误信息  
        }  

        // 创建新的 User 实例  
        User newUser = new User();  
        newUser.setUsername(username);  
        newUser.setPwd(password); // 注意：实际应用中应加密密码  
        newUser.setEmail(email);  
        newUser.setPhone(phone);  
        newUser.setQq(qq);  
        newUser.setInfo(info);  

        // 保存用户到数据库  
        userRespository.save(newUser);  

        // 注册成功后的反馈，可以添加相关信息  
        model.addAttribute("message", "注册成功，请登录！");  
        return "login"; // 注册后重定向到登录页面  
    }  
    @GetMapping(path = "/delete")
	public String delete(@RequestParam long id) {
		userService.deleteById(id);
		return "redirect:/user/page?pageIndex=0";
	}
}
